const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
const app = http.createServer();

app.on('request',(req,res)=>{
  //获取用户的请求路径
  let pathname = url.parse(req.url).pathname;
  pathname = pathname == '/' ? '/about.html' : pathname;
  //将用户的请求路径转换为实际的服务器硬盘路径
  let realPath = path.join(__dirname,'public'+pathname);
  //判断当前请求文件的类型
  let type = mime.getType(realPath);
  //读取文件
  fs.readFile(realPath,(err,result)=>{
    if(err!=null) {
      res.writeHead(404,{
        'content-type':'text/html;charset=utf8',
      })
      res.end('文件读取失败');
      return;
    }
    res.writeHead(200,{
      'content-type':type,
    })
    res.end(result);
  })
})
//监听端口
app.listen(3000);
console.log('网站服务器启动成功');
